package com.ruoyi.controller.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysTenantDB;
import com.ruoyi.system.service.impl.InitTenantServiceImpl;
import com.ruoyi.system.service.impl.SysTenantServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统租户Controller
 *
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController
{
    @Autowired
    private SysTenantServiceImpl sysTenantServiceImpl;

    @Resource
    private InitTenantServiceImpl initTenantServiceImpl;

    /**
     * 查询系统租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenantDB sysTenant)
    {
        startPage();
        List<SysTenantDB> list = sysTenantServiceImpl.selectSysTenantList(sysTenant);
        return getDataTable(list);
    }

    /**
     * 导出系统租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:export')")
    @Log(title = "系统租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTenantDB sysTenant)
    {
        List<SysTenantDB> list = sysTenantServiceImpl.selectSysTenantList(sysTenant);
        ExcelUtil<SysTenantDB> util = new ExcelUtil<SysTenantDB>(SysTenantDB.class);
        util.exportExcel(response, list, "系统租户数据");
    }

    /**
     * 获取系统租户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTenantServiceImpl.selectSysTenantById(id));
    }

    /**
     * 新增系统租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @Log(title = "系统租户", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody SysTenantDB sysTenant)
    {
        // 查询该名称的租户
        sysTenantServiceImpl.insertSysTenant(sysTenant);
        SysTenantDB tenantByPhoneAndName = sysTenantServiceImpl.getTenantByPhoneAndName(sysTenant.getPrimaryPhone(), sysTenant.getTenantName());
        initTenantServiceImpl.init(tenantByPhoneAndName);
        return toAjax(true);
    }

    /**
     * 修改系统租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
    @Log(title = "系统租户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenantDB sysTenant)
    {
        return toAjax(sysTenantServiceImpl.updateSysTenant(sysTenant));
    }

    /**
     * 删除系统租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:remove')")
    @Log(title = "系统租户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysTenantServiceImpl.deleteSysTenantByIds(ids));
    }
}
